/****************************************************************************
 Copyright (c) 2018-2019 Xiamen Yaji Software Co., Ltd.
 Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).

 https://axmol.dev/

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ****************************************************************************/

#pragma once

#include "axmol/rhi/DriverBase.h"
#import <Metal/Metal.h>
#import <QuartzCore/CAMetalLayer.h>

namespace ax::rhi::mtl
{

enum class FeatureSet : int32_t
{
    Unknown                      = -1,
    FeatureSet_iOS_GPUFamily1_v1 = 0,
    FeatureSet_iOS_GPUFamily2_v1 = 1,

    FeatureSet_iOS_GPUFamily1_v2 = 2,
    FeatureSet_iOS_GPUFamily2_v2 = 3,
    FeatureSet_iOS_GPUFamily3_v1 = 4,

    FeatureSet_iOS_GPUFamily1_v3 = 5,
    FeatureSet_iOS_GPUFamily2_v3 = 6,
    FeatureSet_iOS_GPUFamily3_v2 = 7,

    FeatureSet_iOS_GPUFamily1_v4 = 8,
    FeatureSet_iOS_GPUFamily2_v4 = 9,
    FeatureSet_iOS_GPUFamily3_v3 = 10,
    FeatureSet_iOS_GPUFamily4_v1 = 11,

    FeatureSet_iOS_GPUFamily1_v5 = 12,
    FeatureSet_iOS_GPUFamily2_v5 = 13,
    FeatureSet_iOS_GPUFamily3_v4 = 14,
    FeatureSet_iOS_GPUFamily4_v2 = 15,

    FeatureSet_macOS_GPUFamily1_v1 = 10000,

    FeatureSet_macOS_GPUFamily1_v2         = 10001,
    FeatureSet_macOS_ReadWriteTextureTier2 = 10002,

    FeatureSet_macOS_GPUFamily1_v3 = 10003,

    FeatureSet_macOS_GPUFamily1_v4 = 10004,
    FeatureSet_macOS_GPUFamily2_v1 = 10005,
};

/**
 * @addtogroup _metal
 * @{
 */

/**
 * Create resources from MTLDevice.
 */
class DriverImpl : public DriverBase
{
public:
    /* The max vertex attribs, it's not how many device supports which may be lower. */
    static constexpr uint32_t MAX_VERTEX_ATTRIBS = 16;

    /* The vertex data buffers binding index start, the axslcc(SPIRV-Cross), default UBO binding index is 0,
    scope is per stage in MSL
     */
    static constexpr uint32_t VBO_BINDING_INDEX_START = 0;

    /* The vertex instancing buffer binding index */
    static constexpr uint32_t VBO_INSTANCING_BINDING_INDEX = VBO_BINDING_INDEX_START + 1;

    /* The default attribs binding index */
    static constexpr uint32_t DEFAULT_ATTRIBS_BINDING_INDEX = VBO_BINDING_INDEX_START + MAX_VERTEX_ATTRIBS;

    /// @name Constructor, Destructor and Initializers
    DriverImpl();
    ~DriverImpl();

    /// @name Setters & Getters
    /**
     * New a CommandBuffer object.
     * @return A CommandBuffer object.
     */
    CommandBuffer* createCommandBuffer(void*) override;

    /**
     * New a Buffer object.
     * @param size Specifies the size in bytes of the buffer object's new data store.
     * @param type Specifies the target buffer object. The symbolic constant must be BufferType::VERTEX or
     * BufferType::INDEX.
     * @param usage Specifies the expected usage pattern of the data store. The symbolic constant must be
     * BufferUsage::STATIC, BufferUsage::DYNAMIC.
     * @return A Buffer object.
     */
    Buffer* createBuffer(std::size_t size, BufferType type, BufferUsage usage) override;

    /**
     * New a Texture object.
     * @param descriptor Specifies texture description.
     * @return A Texture object.
     */
    Texture* createTexture(const TextureDesc& descriptor) override;

    RenderTarget* createDefaultRenderTarget() override;
    RenderTarget* createRenderTarget(Texture* colorAttachment, Texture* depthStencilAttachment) override;

    /**
     * New a DepthStencilState object.
     */
    DepthStencilState* createDepthStencilState() override;

    /**
     * New a RenderPipeline object.
     * @param descriptor Specifies render pipeline description.
     * @return A RenderPipeline object.
     */
    RenderPipeline* createRenderPipeline() override;

    /**
     * New a Program, not auto release.
     * @param vertexShader Specifes this is a vertex shader source.
     * @param fragmentShader Specifes this is a fragment shader source.
     * @return A Program instance.
     */
    Program* createProgram(std::string_view vertexShader, std::string_view fragmentShader) override;

    /**
     * Get a MTLDevice object.
     * @return A MTLDevice object.
     */
    inline id<MTLDevice> getMTLDevice() const { return _mtlDevice; }

    inline id<MTLCommandQueue> getMTLCmdQueue() const { return _mtlCmdQueue; }

    /// below is driver info

    /// @name Setters & Getters
    /**
     * Get vendor device name.
     * @return Vendor device name.
     */
    std::string getVendor() const override;

    /**
     * Get the full name of the vendor device.
     * @return The full name of the vendor device.
     */
    std::string getRenderer() const override;

    /**
     * Get featureSet name.
     * @return FeatureSet name.
     */
    std::string getVersion() const override;

    /**
     * Check if feature supported by Metal.
     * @param feature Specify feature to be query.
     * @return true if the feature is supported, false otherwise.
     */
    bool checkForFeatureSupported(FeatureType feature) override;

    static bool supportD24S8() { return _isDepth24Stencil8PixelFormatSupported; }

protected:
    /**
     * New a shaderModule.
     * @param stage Specifies whether is vertex shader or fragment shader.
     * @param source Specifies shader source.
     * @return A ShaderModule object.
     */
    ShaderModule* createShaderModule(ShaderStage stage, std::string_view source) override;

    SamplerHandle createSampler(const SamplerDesc& desc) override;
    void destroySampler(SamplerHandle& sampler) override;

private:
    id<MTLDevice> _mtlDevice         = nil;
    id<MTLCommandQueue> _mtlCmdQueue = nil;

    std::string _deviceName;
    FeatureSet _featureSet = FeatureSet::Unknown;
    static bool _isDepth24Stencil8PixelFormatSupported;
};

// end of _metal group
/// @}
}  // namespace ax::rhi::mtl
